package com.woniuxy.cloud.batteryassembly.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.cloud.batteryassembly.mapper.BatteryDamageMapper;
import com.woniuxy.cloud.batteryassembly.model.BatteryDamage;
import com.woniuxy.cloud.batteryassembly.service.IBatteryDamageService;
import com.woniuxy.cloud.batteryassembly.util.WoniuBeanUtils;
import com.woniuxy.cloud.demo.Result;
import com.woniuxy.cloud.dto.DamageDto;
import com.woniuxy.cloud.param.DamageParam;
import com.woniuxy.cloud.param.ListDamageParam;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gaorui
 * @since 2022-02-09
 */
@Service
public class BatteryDamageServiceImpl extends ServiceImpl<BatteryDamageMapper, BatteryDamage> implements IBatteryDamageService {

    @Override
    public  List<DamageDto>  listDamageMessage(ListDamageParam param) {
        //复制新id和旧id
        List<DamageDto> dto = WoniuBeanUtils.copyList(param.getDamageParams(), DamageDto.class);

        List<Integer> newMessage = param.getDamageParams().stream().map(st -> st.getDamageId()).collect(Collectors.toList());
        List<Integer> old = param.getDamageParams().stream().map(st -> st.getInitialDamageId()).collect(Collectors.toList());

        //新
        List<BatteryDamage> newDamages = this.baseMapper.selectBatchIds(newMessage);
        //初始情况
        List<BatteryDamage> initialDamages = this.baseMapper.selectBatchIds(old);

        if (newDamages==null||initialDamages==null){
            return null;
        }

        //开始计算金额,两个相减,传入几个，遍历几次
        for (int i = 0; i < dto.size(); i++) {
            for (int j = 0; j < newDamages.size(); j++) {
                if (dto.get(i).getDamageId()==newDamages.get(j).getDamageId()){
                    dto.get(i).setDamageLevel(newDamages.get(j).getDamageLevel())
                            .setDamageRemark(newDamages.get(j).getDamageRemark())
                            .setDamageIndemnify(newDamages.get(j).getDamageIndemnify());//先设置新的金额比列

                }
            }
        }
        //修改修改比列
        for (int i = 0; i < dto.size(); i++) {
            for (int j = 0; j < initialDamages.size(); j++) {
                if (dto.get(i).getInitialDamageId()==initialDamages.get(j).getDamageId()){
                    Double s =  dto.get(i).getDamageIndemnify()-initialDamages.get(j).getDamageIndemnify();
                    if (s<0) return null;
                    dto.get(i).setDamageIndemnify(s);//先设置新的金额比列,修改
                }
            }
        }
        return dto;
    }
}
